package edu.pdx.LAMA.AndroidCourse.scheduler;

import edu.pdx.LAMA.AndroidCourse.ModelLayer.EventService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddCategoryActivity extends Activity {

	private EditText categoryName;
	private Button createCategory;
	private EventService eventService;

	private static final String SAVER_NEW_CATEGORY = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVERCATEGORY";

	long value;

	AlertDialog.Builder builder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_category);

		categoryName = (EditText) findViewById(R.id.newCategory);
		createCategory = (Button) findViewById(R.id.createCategoryButton);
		createCategory.setText("Create Category");
		createCategory.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				eventService = new EventService(getApplicationContext());
				value = eventService.addCategory(categoryName.getText().toString());
				if (value == -1){
					showExistingEventDialog();
				}
				else{
					Intent ix = new Intent(getApplicationContext(), ScheduleActivity.class);
					startActivity(ix);
				}
			}
		});

		if(savedInstanceState != null){
			categoryName.setText(savedInstanceState.getString(SAVER_NEW_CATEGORY));
		}
	}

	private void showExistingEventDialog() {
		// TODO Auto-generated method stub
		builder = new AlertDialog.Builder(this);
		builder.setTitle("Estimated Travel Time");
		builder.setMessage("This category already exists");
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		builder.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_category, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putString(SAVER_NEW_CATEGORY, categoryName.getText().toString());
	}
}
